clearvars -except Globaloption option

if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

warning off

%%%1
data = readtable('../../data/Netherlands/Holland_PF_v2.xlsx');

startdatenum = 1601;
enddatenum = 1794;

data = data((data.Var1 >= startdatenum), :);
data = data((data.Var1 <= enddatenum), :);

tau1 = data.tau; % government tax revenue to GDP ratio
g1 = data.g; % government spending before interest exp. to GDP ratio
debt_gdp1 = data.debt_gdp;
%%%2

data = readtable('../../data/Netherlands/NetherlandsPF_1815_1938.xlsx');
date = data.year;

startdatenum = 1817;
enddatenum = 1938;

data = data((data.year >= startdatenum), :);
data = data((data.year <= enddatenum), :);

tau2 = data.tau; % government tax revenue to GDP ratio
g2 = data.g; % government spending before interest exp. to GDP ratio
debt_gdp2 = data.debt_gdp / 100;

%%%% organize
tau = [tau1; nan(1817 - 1794 - 1, 1); tau2];
g = [g1; nan(1817 - 1794 - 1, 1); g2];
surplus = tau - g;
startdatenum = 1601;
ttime = (startdatenum:1938)'
figures.path = ['figures\netherland_'];
date = 1601:1938';

debt = [debt_gdp1; nan(1817 - 1794 - 1, 1); debt_gdp2];

save ('MAT/holland_rawdata.mat');

%%

load ('MAT/holland_rawdata.mat');

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on
set(gca, 'FontSize', 14)
ylim([-20 50])
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
hold on
patch([date(find(date == 1830)) date(find(date == 1830)) date(find(date == 1839)) date(find(date == 1839))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1665)) date(find(date == 1665)) date(find(date == 1667)) date(find(date == 1667))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1652)) date(find(date == 1652)) date(find(date == 1654)) date(find(date == 1654))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1672)) date(find(date == 1672)) date(find(date == 1674)) date(find(date == 1674))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1701)) date(find(date == 1701)) date(find(date == 1714)) date(find(date == 1714))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1688)) date(find(date == 1688)) date(find(date == 1697)) date(find(date == 1697))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1740)) date(find(date == 1740)) date(find(date == 1751)) date(find(date == 1751))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1785)) date(find(date == 1785)) date(find(date == 1795)) date(find(date == 1795))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

%
ha = plot(ttime, 100 * surplus, 'black', 'LineWidth', 2);
hold on

ht = plot(ttime, 100 * tau, '--blue', 'LineWidth', 1.5);

hg = plot(ttime, 100 * g, '-.red', 'LineWidth', 1.5);
xlim([startdatenum 1940])
hleglines = [ha ht hg];
hleg = legend(hleglines, 'Surplus/GDP', 'Revenue/GDP', 'Spending/GDP');
grid on
ylabel('% of GDP')

gtext('1st')
gtext('Anglo-Dutch')
gtext('War')
gtext('2nd')
gtext('Anglo-Dutch')
gtext('War')
gtext('3rd')
gtext('Anglo-Dutch')
gtext('War')
gtext('Nine Years War')

gtext('War of the Spanish')
gtext('Succession')

gtext('War of the Austrian Succession')
gtext('Batavian Revolution')
gtext('Belgian Revolution')
gtext('WW I')

set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
f.PaperSize = [10 5];

if Globaloption.savefig == 1
    print('../../results/figures_main/Netherlands/H_surpluses', '-dpdf', '-fillpage')
end

%% debt to gdp ratio
f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on
set(gca, 'FontSize', 14)
ylim([0 300])
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
hold on
patch([date(find(date == 1830)) date(find(date == 1830)) date(find(date == 1839)) date(find(date == 1839))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1665)) date(find(date == 1665)) date(find(date == 1667)) date(find(date == 1667))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1672)) date(find(date == 1672)) date(find(date == 1674)) date(find(date == 1674))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1701)) date(find(date == 1701)) date(find(date == 1714)) date(find(date == 1714))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1688)) date(find(date == 1688)) date(find(date == 1697)) date(find(date == 1697))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1740)) date(find(date == 1740)) date(find(date == 1751)) date(find(date == 1751))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1785)) date(find(date == 1785)) date(find(date == 1795)) date(find(date == 1795))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1652)) date(find(date == 1652)) date(find(date == 1654)) date(find(date == 1654))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

ha = plot(ttime, 100 * debt, 'black', 'LineWidth', 1.5);
hold on
xlim([startdatenum 1940])
hleglines = [ha];
hleg = legend(hleglines, 'Debt/GDP(Book Value)');
grid on
ylabel('% of GDP')

grid on
ylabel('% of GDP')

gtext('1st')
gtext('Anglo-Dutch')
gtext('War')
gtext('2nd')
gtext('Anglo-Dutch')
gtext('War')
gtext('3rd')
gtext('Anglo-Dutch')
gtext('War')
gtext('Nine Years War')

gtext('War of the Spanish')
gtext('Succession')

gtext('War of the Austrian Succession')
gtext('Batavian Revolution')
gtext('Belgian Revolution')
gtext('WW I')

set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
f.PaperSize = [10 5];

if Globaloption.savefig == 1
    print('../../results/figures_main/Netherlands/H_debtgdp', '-dpdf', '-fillpage')
end
